package com.pi.http.base.model;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.lang3.StringUtils;

/**
 * @author chenmfa
 * @description 过滤request的特殊字符和前后空格
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper{
  
  private Map<String, String[]> params = new HashMap<String, String[]>();;
  private boolean filterEmoji = true;
  private boolean filterValue = true;
  public ParameterRequestWrapper(HttpServletRequest request,boolean isFilter, boolean filterValue) {
    super(request);
    this.filterEmoji = isFilter;
    this.filterValue = filterValue;
    renewParameterMap(request);    
  }
  
  /**
   * @description 更新request参数
   * @return
   */
	private void renewParameterMap(HttpServletRequest request) {
    Map<String,String[]> paramMap = super.getParameterMap();
    if(null != paramMap){
      for(Map.Entry<String, String[]> entry : paramMap.entrySet()){
        String name = entry.getKey();
        String values[] = entry.getValue();
        if(values==null){
          params.put(name, new String[]{});
          continue;
        }
        String newvalues[] = new String[values.length];
        for(int i=0; i<values.length;i++){
          newvalues[i] = (null == values[i])?"":paramValueClean(values[i]); 
        } 
        params.put(name, newvalues);
      }
    }
  }


  @Override
  public Map<String, String[]> getParameterMap() {
      return params;
  }

  @Override
  public Enumeration<String> getParameterNames() {
      return new Vector<String>(params.keySet()).elements();
  }

  @Override
  public String getParameter(String parameter) {
    String[] paramValues = params.get(parameter);
    if (paramValues == null || paramValues.length ==0) {
        return  null;
    } else{
      return paramValues[0];
    }
  }
  
  @Override
  public String[] getParameterValues(String parameter) {
    return params.get(parameter);
  }
  
  @Override
  public String getHeader(String name) {   
      return paramValueClean(super.getHeader(name));  
  }
  
  private String paramValueClean(String value){
    return filterEmoji(cleanXss(StringUtils.trim(value), filterEmoji, filterValue), filterEmoji);
  }
  
  /**
   * @description 过滤非法字符
   * @return
   */
  private static String cleanXss(String value, boolean filterEmoji, boolean filterValue){
    if(StringUtils.isNotBlank(value)){
      if(!filterValue){
        return value;
      }
      if(filterEmoji){
        return value
            .replaceAll("<", "&lt;").replaceAll(">", "&gt;")
//            .replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;")
            .replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"")
            .replaceAll("eval\\((.*)\\)", "")
            .replaceAll("\u0025", "\u005b\u0025\u005d");
      }else{
        return value
            .replaceAll("<", "&lt;").replaceAll(">", "&gt;")
//            .replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;")
            .replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"")
            .replaceAll("eval\\((.*)\\)", "").replaceAll("\"", "&quot;").replaceAll("'", "&apos;")
            .replaceAll("\u0025", "\u005b\u0025\u005d");
      }
    }else{
      return null;
    }
  }
  
  /**
   * @description 过滤emoji 表情
   * @return
   */
  public static String filterEmoji(String source, boolean filterEmoji) { 
    if(source != null && filterEmoji){
      Pattern emoji = Pattern.compile ("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",Pattern.UNICODE_CASE | Pattern . CASE_INSENSITIVE ) ;
      Matcher emojiMatcher = emoji.matcher(source);
      if (emojiMatcher.find()){
        source = emojiMatcher.replaceAll("[emoji]");
        return source ;
      }
      return source;
    }
    return source; 
  }
}
